#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
const LL mod=1e9+7;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL ksm(LL a,LL k){LL res=1;while(k>0){if(k&1) res=res*a%mod;k>>=1;a=a*a%mod;}return res%mod;}
LL a[maxn],siz[maxn];
vector<LL>g[maxn];
LL col[maxn],dfn[maxn],low[maxn],tot=0,cnt=0;
LL fa[maxn];
bool instack[maxn];
stack<LL>s;
struct Edge{
    LL u,v;
}Edges[maxn];
inline bool operator<(const Edge&a, const Edge&b) {return a.u != b.u ? a.u < b.u : a.v < b.v;}
void tarjan(LL u){
     dfn[u]=low[u]=++tot;
     s.push(u);
     instack[u]=true;
     for(LL i=0;i<g[u].size();i++){
         LL v=g[u][i];
         if(v==fa[u]) continue;
         if(!dfn[v]){
            fa[v]=u;
            tarjan(v);
            low[u]=min(low[u],low[v]);
         }
         else if(instack[v]){
            low[u]=min(low[u],dfn[v]);
         }
     }
     if(dfn[u]==low[u]){
        cnt++;
        LL y;
        do{
           y=s.top();
           col[y]=cnt;
           instack[y]=false;
           siz[u]+=siz[y];
           s.pop();
        }while(y!=u);
     }
     return;
}
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n;cin>>n;
   for(LL i=1;i<=n;i++){
        LL to;cin>>to;
        Edges[i]=Edge{min(i,to),max(i,to)};
   }
   sort(Edges+1,Edges+1+n);
   for(LL i=1;i<=n;i++){
       if(Edges[i].u!=Edges[i+1].u||Edges[i].v!=Edges[i+1].v){
            g[Edges[i].u].push_back(Edges[i].v);
  ///          g[Edges[i].v].push_back(Edges[i].u);
       }
   }
   for (int i = 1; i <= n; ++i) siz[i] = 1;
   for(LL i=1;i<=n;i++){
       if(!dfn[i]){
          tarjan(i);
       }
   }
   ///LL edge=0;
   LL m=n;
   LL ans=1;
   for(LL i=1;i<=n;i++){
       if(col[i]==i&&siz[i]>1){
         ans=(ans%mod*ksm(2,siz[i]%mod)%mod-2+mod)%mod;
         m-=siz[i];
       }
   }
   ans=(ans%mod*ksm(2,m)%mod)%mod;
   cout<<ans<<"\n";
   return 0;
}
